﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Reflection
Imports System.Workflow.ComponentModel.Serialization

Namespace System.Workflow.ComponentModel.Design
    Friend NotInheritable Class PointMarkupSerializer
        Inherits WorkflowMarkupSerializer
        ' Methods
        Protected Friend Overrides Function CanSerializeToString(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal value As Object) As Boolean
            Return TypeOf value Is Point
        End Function

        Protected Friend Overrides Function DeserializeFromString(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal propertyType As Type, ByVal value As String) As Object
            Dim empty As Object = Point.Empty
            Dim str As String = value
            If String.IsNullOrEmpty(str) Then
                Return empty
            End If
            Dim converter As TypeConverter = TypeDescriptor.GetConverter(GetType(Point))
            If (((Not converter Is Nothing) AndAlso converter.CanConvertFrom(GetType(String))) AndAlso Not MyBase.IsValidCompactAttributeFormat(str)) Then
                Return converter.ConvertFrom(value)
            End If
            Return MyBase.SerializeToString(serializationManager, value)
        End Function

        Protected Friend Overrides Function GetProperties(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal obj As Object) As PropertyInfo()
            Dim list As New List([Of] PropertyInfo)
            If TypeOf obj Is Point Then
                list.Add(GetType(Point).GetProperty("X"))
                list.Add(GetType(Point).GetProperty("Y"))
            End If
            Return list.ToArray
        End Function

        Protected Friend Overrides Function SerializeToString(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal value As Object) As String
            Dim converter As TypeConverter = TypeDescriptor.GetConverter(value)
            If ((Not converter Is Nothing) AndAlso converter.CanConvertTo(GetType(String))) Then
                Return [TryCast](converter.ConvertTo(value, GetType(String)),String)
            End If
            Return MyBase.SerializeToString(serializationManager, value)
        End Function

    End Class
End Namespace

